package com.lipeng.service.impl;

import com.lipeng.bean.Collect;
import com.lipeng.bean.Order;
import com.lipeng.bean.Shop;
import com.lipeng.mapper.CollectMapper;
import com.lipeng.mapper.OrderMapper;
import com.lipeng.mapper.ShopMapper;
import com.lipeng.service.ShopService;
import com.lipeng.utils.AjaxResult;
import com.lipeng.utils.IPUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 18/3/7.
 */
@Service
public class ShopServiceImpl implements ShopService {
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private CollectMapper collectMapper;
    @Resource
    private OrderMapper orderMapper;



    @Override
    public Shop selectShopById(Integer sid) {
        return shopMapper.selectByPrimaryKey(sid);
    }

    @Override
    public List<Shop> selectAllShop() {
        return shopMapper.selectAllShop();
    }

    @Override
    public int insertShop(Shop shop) {
        return shopMapper.insert(shop);
    }

    @Override
    public int updateShop(Shop shop) {
        return shopMapper.updateByPrimaryKey(shop);
    }

    @Override
    public int deleteShopById(Integer sid) {
        return shopMapper.deleteByPrimaryKey(sid);
    }

    @Override
    public List<Shop> selectShopByName(String name) {
        return shopMapper.selectShopByName(name);
    }

    @Override
    public AjaxResult addCollect(Integer uid, Integer sid) {
        AjaxResult result = new AjaxResult();
        Collect collect = new Collect();
        collect.setUid(uid);
        collect.setSid(sid);
        Integer num = collectMapper.insert(collect);
        if (num <1){
            result.setCode("0");
            result.setMessage("收藏失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("收藏成功");
        result.setData(collect.getColid());
        return result;
    }

    @Override
    public AjaxResult delCollect(Integer uid,Integer sid) {
        AjaxResult result = new AjaxResult();
        List<Collect> collectList = collectMapper.findCollect(sid,uid);
        int colid = 0;
        if (!collectList.isEmpty()){
            for(Collect collect : collectList){
                colid = collect.getColid();
            }
        }
        Integer num = collectMapper.deleteByPrimaryKey(colid);
        if (num <1){
            result.setCode("0");
            result.setMessage("取消收藏失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("取消收藏成功");
        return result;
    }

    @Override
    public AjaxResult selectCollectByUid(Integer uid) {
        AjaxResult result = new AjaxResult();
        List<Collect> collectList = collectMapper.selectCollectByUid(uid);
        List<Shop> shopList = new ArrayList<Shop>();
        for (Collect collect : collectList){
            Shop shop = collect.getShop();
            shopList.add(shop);
        }
        for (Shop shop : shopList){
            shop.setSpic(IPUtils.getIP()+":8080"+shop.getSpic());
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(shopList);
        return result;
    }

    @Override
    public AjaxResult addOrder(Integer uid, Integer sid, Integer count) {
        AjaxResult result = new AjaxResult();
        Order order = new Order();
        order.setUid(uid);
        order.setSid(sid);
        order.setCount(count);
        order.setStatus(0);
        int num = orderMapper.insert(order);
        if (num<1){
            result.setCode("0");
            result.setMessage("添加失败");
        }
        result.setCode("1");
        result.setMessage("添加成功");
        result.setData(order.getOid());
        return result;
    }

    @Override
    public AjaxResult payOrder(Integer oid) {
        AjaxResult result = new AjaxResult();
        int num = orderMapper.payOrder(oid);
        if (num<1){
            result.setCode("0");
            result.setMessage("付款失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("付款成功");
        return result;
    }

    @Override
    public AjaxResult delOrder(Integer oid) {
        AjaxResult result = new AjaxResult();
        Integer num = orderMapper.deleteByPrimaryKey(oid);
        if (num<1){
            result.setCode("0");
            result.setMessage("取消失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("取消成功");
        return result;
    }

    @Override
    public List<Collect> findCollect(Integer sid, Integer uid) {
        return collectMapper.findCollect(sid,uid);
    }


}
